home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
TCL1
/
__MANDEL
/
MANDELBR
/
CMANDELD.C1
< prev
next >
Wrap
Text File
|
1992-03-25
|
2KB
|
142 lines
// CMandelDataFile.c
#include "CMandelDataFile.h"
#include "CMandelDoc.h"
#define MIN_RUN 3
void
CMandelDataFile::IMandelDataFile(CMandelDoc *theMandelDoc)
{
CDataFile::IDataFile();
itsMandelDoc = theMandelDoc;
}
void
CMandelDataFile::WriteAll(Handle theDataH)
{
TMandelInfo aInfoRec;
TDwellH aDwellH;
TDwell n, *p, *q;
itsMandelDoc->GetMandelInfo(&aInfoRec);
WriteSome((Ptr)&aInfoRec, sizeof(aInfoRec));
/* run-length encoding...
a run can be either two-word unit where word 0 is a count and word 1 is
the value to be repeated for count, or a n-word unit where word 0 is a
count and the following n-1 words are taken literally.
the first run is of the former.
*/
aDwellH = itsMandelDoc->GetDwellsHandle();
HLock(aDwellH);
p = *aDwellH;
q = p + GetHandleSize(aDwellH) / sizeof(TDwell);
while (p < q)
{
n = cGetRunSize(p, q);
if (n >= MIN_RUN)
{
TDwell aRun[2];
aRun[0] = n;
aRun[1] = *p;
WriteSome((Ptr)aRun, sizeof(aRun));
p += n;
}
else
{
n = 0;
WriteSome((Ptr)&n, sizeof(n));
}
n = cGetNurSize(p, q);
WriteSome((Ptr)&n, sizeof(n));
WriteSome((Ptr)p, sizeof(*p) * n);
p += n;
}
HUnlock(aDwellH);
n = 0;
WriteSome((Ptr)&n, sizeof(n));
WriteSome((Ptr)&n, sizeof(n));
WriteSome((Ptr)&n, sizeof(n));
}
Handle CMandelDataFile::ReadAll(void)
{
TMandelInfo aInfoRec;
TDwellH aDwellsH;
TDwell aDwell, i, n, *p, *q;
ReadSome((Ptr)&aInfoRec, sizeof(aInfoRec));
itsMandelDoc->SetMandelInfo(&aInfoRec);
aDwellsH = itsMandelDoc->GetDwellsHandle();
HLock(aDwellsH);
p = *aDwellsH;
q = p + GetHandleSize(aDwellsH) / sizeof(TDwell);
while (p < q)
{
ReadSome((Ptr)&n, sizeof(n));
if (n)
ReadSome((Ptr)&aDwell, sizeof(aDwell));
for (i = 0; i < n; i++)
*p++ = aDwell;
ReadSome((Ptr)&n, sizeof(n));
ReadSome((Ptr)p, sizeof(*p) * n);
p += n;
}
HUnlock(aDwellsH);
itsMandelDoc->DoDraw();
return NULL;
}
unsigned
CMandelDataFile::cGetRunSize(unsigned *p, unsigned *q)
{
unsigned long i, n;
n = q - p;
if (n > 65535)
n = 65535;
for (i = 1; i < n; i++)
{
if (p[i] != *p)
break;
}
return i;
}
unsigned
CMandelDataFile::cGetNurSize(unsigned *p, unsigned *q)
{
unsigned long i, n;
n = q - p;
if (n > 65535)
n = 65535;
for (i = 0; i < n; i++)
{
if (cGetRunSize(p + i, q) >= MIN_RUN)
break;
}
return i;
}